// wenshidu/add_chufang/add_chufang.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
var shuzong = []
var shu = []
Page({
    ...request,
    /**
     * 页面的初始数据
     */
    data: {
        yuming: yuming,
        title: '',
        state: '', //0添加 1修改 2添加未上传处方说明 3修改未上传处方
        heights: '',
        list_gao: '',
        sctime: '',
        sctimes: '',
        endtime: '',
        endtimes: '',
        photo: [],
        wuphoto: [],
        id: '',
        hourMinuteSecond: '请选择结束时间', //时分，根据需要选择
        timeDivision: '请选择开始时间', //时分秒，根据需要选择
        remark: ''
    },
    bindremark(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    time_to_sec(time) {
        if (time !== null) {
            var s = "";
            var hour = time.split(":")[0];
            var min = time.split(":")[1];
            var sec = time.split(":")[2];
            s = Number(hour * 3600) + Number(min * 60) + Number(sec);
            return s;
        }
    },
    // 结束
    selectDateMinuteChange(e) {
        var aa = this.time_to_sec(e.detail.value)
        console.log(aa)
        var bb = this.time_to_sec(this.data.timeDivision)
        console.log(bb)
        if (aa < bb) {
            wx.showToast({
                title: '结束时间不应小于开始时间',
                icon: 'none'
            })
        } else {
            this.setData({
                hourMinuteSecond: e.detail.value
            })
        }
    },
    // 时分秒的事件方法
    selectDateSecondChange(e) {
        this.setData({
            timeDivision: e.detail.value
        })
    },
    bindsct(e) {
        this.setData({
            sctime: e.detail.value,
            sctimes: e.detail.value,
        })
    },
    bindend(e) {
        this.setData({
            endtime: e.detail.value,
            endtimes: e.detail.value,
        })
    },
    tijiao() {
        if (this.data.timeDivision == '' || this.data.timeDivision == '请选择开始时间') {
            wx.showToast({
                title: '请选择开始时间',
                icon: 'none'
            })
        } else if (this.data.hourMinuteSecond == '' || this.data.hourMinuteSecond == '请选择结束时间') {
            wx.showToast({
                title: '请选择结束时间',
                icon: 'none'
            })
        } else if (this.data.photo.length == 0 && this.data.state!=2  && this.data.state!=3 ) {
            wx.showToast({
                title: '请上传图片',
                icon: 'none'
            })
        } else {
            this.jiekou()
        }
    },
    jiekou() {
        this.setData({
            buttonClicked: true
        })
        var wuphoto = this.data.wuphoto.toString()
        var url = ''
        var data = {}
        if (this.data.state == 0) {
            url = 'Jinghexincheng/jhxc_chufangyao_bind'
            data = {
                shopId: wx.getStorageSync('shopId'),
                uptime: this.data.timeDivision,
                endtime: this.data.hourMinuteSecond,
                images: wuphoto,
                remark: this.data.remark,
            }
        }
        if (this.data.state == 1) {
            url = 'Jinghexincheng/jhxc_chufangyao_save'
            data = {
                id: this.data.id,
                shopId: wx.getStorageSync('shopId'),
                uptime: this.data.timeDivision,
                endtime: this.data.hourMinuteSecond,
                images: wuphoto,
                remark: this.data.remark,
            }
        }
        if (this.data.state == 2) {//新增未上传说明
          url = 'Jinghexincheng/chufangyao_nolog_add'
          data = {
              shopId: wx.getStorageSync('shopId'),
              signtime: this.data.timeDivision,
              endtime: this.data.hourMinuteSecond,
              explain_image: wuphoto,
              notexplain: this.data.remark,
          }
      }
      if (this.data.state == 3) {//新增未上传说明
        url = 'Jinghexincheng/chufangyao_nolog_save'
        data = {
            shopId: wx.getStorageSync('shopId'),
            signtime: this.data.timeDivision,
            endtime: this.data.hourMinuteSecond,
            explain_image: wuphoto,
            notexplain: this.data.remark,
            id:this.data.id
        }
    }
        this.myRequest(yuming + url, data, 'POST').then(res => {
            wx.hideLoading();
            wx.showToast({
                title: res.data.msg,
                icon: 'none'
            })
            if (res.data.code == 200) {
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 0,
                    })
                }, 1000)
            } else {
                this.setData({
                    buttonClicked: false
                })
            }
        })
    },
    cfxq() {
        this.myRequest(yuming + 'Jinghexincheng/jhxc_chufangyao_details', {
            shopId: wx.getStorageSync('shopId'),
            id: this.data.id
        }, 'POST').then(res => {
            var data = res.data.data
            this.setData({
                photo: data.images,
                wuphoto: data.images_thumb,
                timeDivision: data.s_uptime,
                hourMinuteSecond: data.s_endtime,
                remark: data.remark
            })
            if (data.remark == null) {
                this.setData({
                    remark: ''
                })
            }
        })
    },
    weicfxq() {
      this.myRequest(yuming + 'Jinghexincheng/chufangyao_nolog_info', {
          shopId: wx.getStorageSync('shopId'),
          id: this.data.id
      }, 'POST').then(res => {
          var data = res.data.data
          this.setData({
              photo: data.explain_image_thumb,
              wuphoto: data.explain_image,
              timeDivision: data.signtime,
              hourMinuteSecond: data.endtime,
              remark: data.notexplain
          })
          if (data.notexplain == null) {
              this.setData({
                  remark: ''
              })
          }
          if (data.explain_image == '') {
            this.setData({
              photo: [],
              wuphoto: []
            })
        }
   
      })
  },
    // 放大
    fangda(e) {
        var img = e.currentTarget.dataset.img
        var list = e.currentTarget.dataset.list
        var arr = []
        for (let i in list) {
            arr.push(list[i]); //属性
        }
        wx.previewImage({
            current: img,
            urls: arr
        })
    },
    // 差掉
    chadiao(e) {
        var index = e.currentTarget.dataset.index
        var photo = this.data.photo;
        var wuphoto = this.data.wuphoto;
        photo.splice(index, 1)
        wuphoto.splice(index, 1)
        this.setData({
            photo: photo,
            wuphoto: wuphoto
        })
    },
    // 上传
    chuan(e) {
        var that = this
        var pics = [];
        var wuphoto = this.data.wuphoto;
        this.myChooseImage(9).then(res => {
            console.log(res)
            const tempFilePaths = res.tempFilePaths
            for (var i = 0; i < tempFilePaths.length; i++) {
                pics.push(tempFilePaths[i])
            }
            if ((pics.length) + (that.data.photo.length) > 9) {
                wx.showToast({
                    title: '最多可上传' + 9 + "张",
                    icon: 'none'
                })
            } else {
                for (var i = 0; i < tempFilePaths.length; i++) {
                    (function (i) { // 注意这里是形参
                        setTimeout(function () {
                            that.duoupimg(tempFilePaths[i],1).then(res => {
                                wx.showLoading({
                                    title: '上传图片中...',
                                });
                                var data = JSON.parse(res.data)
                                var urls = data.httpUrl + data.imgUrl
                                var wuurls = data.imgUrl
                                // wuphoto = wuphoto.concat(data.imgUrl + ",")
                                that.setData({
                                    photo: that.data.photo.concat(urls),
                                    wuphoto: that.data.wuphoto.concat(wuurls),
                                });
                                if (i == tempFilePaths.length - 1) {
                                    wx.hideLoading();
                                }
                            })
                        }, 800 * i); // 还是每秒执行一次，不是累加的
                    })(i) // 注意这里是实参，这里把要用的参数传进去
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            state: options.state,
            heights: wx.getMenuButtonBoundingClientRect().top + 5,
            list_gao: wx.getMenuButtonBoundingClientRect().top + 50,
            id: options.id
        })
        if (options.state == 0) {
            this.setData({
                title: '添加处方'
            })
        }
        if (options.state == 1) {
            this.setData({
                title: '修改处方'
            })
            this.cfxq()
        }
        if (options.state == 2) {
            this.setData({
                title: '添加未上传处方说明'
            })
        }
        if (options.state == 3) {
          this.setData({
              title: '修改未上传处方说明'
          })
          this.weicfxq()
      }
    },
    // 返回
    backs() {
        wx.navigateBack({
            delta: 0
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})